package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.seller.ItemOrderInfo;
import com.zhongxi.guoyuan.entity.seller.OrderInfoDdBean;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2018/1/13.
 */

public class SellerExchangeAdapter extends BaseAdapter {

    private Handler handler;
    private Context context;
    private List<OrderInfoDdBean> list;
    private int ordertype;

    public SellerExchangeAdapter(Context context, List<OrderInfoDdBean> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return (list != null && list.size() > 0) ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        try {
            final ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = View.inflate(context, R.layout.item_post_silver_order, null);
                vh.ll_base = (LinearLayout) view.findViewById(R.id.ll_base);
                vh.tv_orderId = (TextView) view.findViewById(R.id.tv_orderId);
                vh.tv_orderNum = (TextView) view.findViewById(R.id.tv_orderNum);
                vh.tv_orderTime = (TextView) view.findViewById(R.id.tv_orderTime);
                vh.tv_prepare = (TextView) view.findViewById(R.id.tv_prepare);

                vh.rl_postInfo = (RelativeLayout) view.findViewById(R.id.rl_postInfo);
                vh.tv_postId = (TextView) view.findViewById(R.id.tv_postId);
                vh.tv_flag = (TextView) view.findViewById(R.id.tv_flag);
                vh.ll_complain = (LinearLayout) view.findViewById(R.id.ll_complain);
                vh.tv_complain_time = (TextView) view.findViewById(R.id.tv_complain_time);
                vh.tv_complain = (TextView) view.findViewById(R.id.tv_complain);

                vh.lvList = (NoScrollListView) view.findViewById(R.id.lvList);

                vh.tv_md = (TextView) view.findViewById(R.id.tv_md);
                vh.tv_hd = (TextView) view.findViewById(R.id.tv_hd);
                vh.tv_price = (TextView) view.findViewById(R.id.tv_price);
                vh.ll_total_postage = (LinearLayout) view.findViewById(R.id.ll_total_postage);
                vh.tv_total_postage = (TextView) view.findViewById(R.id.tv_total_postage);
                vh.tv_orderPhone = (TextView) view.findViewById(R.id.tv_orderPhone);
                vh.tv_call = (TextView) view.findViewById(R.id.tv_call);

                vh.rl_address_info = (RelativeLayout) view.findViewById(R.id.rl_address_info);
                vh.tvUserName = (TextView) view.findViewById(R.id.tvUserName);
                vh.tvPhone = (TextView) view.findViewById(R.id.tvPhone);
                vh.tvAddress = (TextView) view.findViewById(R.id.tvAddress);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            final OrderInfoDdBean orderInfos = list.get(position);
            if (null != orderInfos.getId() && !"".equals(orderInfos.getId())) {
                vh.tv_orderId.setText("订单编号:  " + orderInfos.getId());
            } else {
                vh.tv_orderId.setText("");
            }
            if (null != orderInfos.getGoodsnum() && !"".equals(orderInfos.getGoodsnum())) {
                vh.tv_orderNum.setText("共 " + orderInfos.getGoodsnum() + " 件");
            } else {
                vh.tv_orderNum.setText("");
            }
            if (null != orderInfos.getInnerprice() && !"".equals(orderInfos.getInnerprice())) {
                vh.tv_md.setText(orderInfos.getInnerprice() + "美豆");
            } else {
                vh.tv_md.setText("");
            }
            if (null != orderInfos.getRedbean() && !"".equals(orderInfos.getRedbean())) {
                if (DataUtil.moreThanZero(orderInfos.getRedbean())) {
                    vh.tv_hd.setText("+" + orderInfos.getRedbean() + "红豆");
                } else {
                    vh.tv_hd.setText("");
                }
            } else {
                vh.tv_hd.setText("");
            }
            if (null != orderInfos.getPrice() && !"".equals(orderInfos.getPrice())) {
                if (DataUtil.moreThanZero(orderInfos.getPrice())) {
                    vh.tv_price.setText("+" + orderInfos.getPrice() + "元");
                } else {
                    vh.tv_price.setText("");
                }
            } else {
                vh.tv_price.setText("");
            }

            if (null != orderInfos.getPostage() && !"".equals(orderInfos.getPostage())) {
                vh.tv_total_postage.setText("快递:  " + orderInfos.getPostage() + " 元");
            } else {
                vh.tv_total_postage.setText("");
            }
            if (!TextUtils.isEmpty(orderInfos.getUserPhone())) {
                vh.tv_orderPhone.setText("下单账号  ：" + orderInfos.getUserPhone());
                vh.tv_call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + orderInfos.getUserPhone());
                        intent.setData(data);
                        context.startActivity(intent);
                    }
                });
            } else {
                vh.tv_orderPhone.setText("");
            }
            JSONObject express = new JSONObject(MyApplication.getInstance().getLogistics());
            if (!TextUtils.isEmpty(orderInfos.getConsignee())) {
                vh.tvUserName.setText(orderInfos.getConsignee());
            } else {
                vh.tvUserName.setText("");
            }
            if (!TextUtils.isEmpty(orderInfos.getConsigneemphone())) {
                vh.tvPhone.setText(orderInfos.getConsigneemphone());
            } else {
                vh.tvPhone.setText("");
            }
            if (!TextUtils.isEmpty(orderInfos.getShippingaddress())) {
                vh.tvAddress.setText(orderInfos.getShippingaddress());
            } else {
                vh.tvAddress.setText("");
            }
            if (!TextUtils.isEmpty(orderInfos.getShippercode())) {
                String LogisticComp = express.optString(orderInfos.getShippercode());
                vh.tv_postId.setText(LogisticComp + "(运单号): " + orderInfos.getLogisticscode());
            } else {
                vh.tv_postId.setText("");
            }

            vh.lvList.setAdapter(new PostitemOrderListAdapter(new ItemOrderInfo(orderInfos.getOrdertype(), orderInfos.getGoodsname(),
                    orderInfos.getGoodsnum(), orderInfos.getThumbnail(), orderInfos.getRedbeanunitprice(), orderInfos.getInnerunitprice(), orderInfos.getGattr())));

            if (!TextUtils.isEmpty(orderInfos.getComplain())) {
                vh.tv_complain.setText(orderInfos.getComplain());
            } else {
                vh.tv_complain.setText("");
            }

            if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                vh.tv_complain_time.setText(orderInfos.getPaytime());
            } else {
                vh.tv_complain_time.setText("");
            }
            switch (ordertype) {
                case 1:  // 自提
                    vh.rl_postInfo.setVisibility(View.GONE);
                    vh.ll_complain.setVisibility(View.GONE);
                    vh.ll_total_postage.setVisibility(View.GONE);
                    vh.rl_address_info.setVisibility(View.GONE);
                    vh.ll_base.setBackgroundResource(R.color.bg_time);
                    if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                        vh.tv_orderTime.setText("下单时间:  " + orderInfos.getCtime());
                    } else {
                        vh.tv_orderTime.setText("");
                    }
                    if (0 == orderInfos.getOrderstatus()) {
                        vh.tv_prepare.setText("未备货");
                        vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.white));
                        vh.tv_prepare.setBackgroundResource(R.drawable.bg_orange_soild);
                        vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认执行此操作吗？")
                                        .setPositiveButton("确认执行", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Message message = new Message();
                                                message.what = 1;
                                                Bundle bundle = new Bundle();
                                                bundle.putString("orderid", orderInfos.getId());
                                                bundle.putString("shopid", orderInfos.getShopid());
                                                bundle.putInt("flag", 1);
                                                bundle.putInt("pos", position);
                                                message.setData(bundle);
                                                handler.sendMessage(message);
                                                dialog.dismiss();
                                            }
                                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        }).show();
                                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                            }
                        });
                    } else if (1 == orderInfos.getOrderstatus()) {
                        vh.tv_prepare.setText("已备货");
                        vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.postCartItem));
                        vh.tv_prepare.setBackgroundColor(context.getResources().getColor(R.color.bg_time));
                        vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        });
                    } else if (2 == orderInfos.getOrderstatus()) {
                        vh.tv_prepare.setText("已领取");
                        vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.postCartItem));
                        vh.tv_prepare.setBackgroundColor(context.getResources().getColor(R.color.bg_time));
                        vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        });
                    }
                    break;
                case 2:  //邮寄
                    vh.rl_postInfo.setVisibility(View.GONE);
                    vh.ll_complain.setVisibility(View.GONE);
                    vh.ll_total_postage.setVisibility(View.VISIBLE);
                    vh.rl_address_info.setVisibility(View.VISIBLE);
                    vh.ll_base.setBackgroundResource(R.color.cart_background);
                    if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                        vh.tv_orderTime.setText("下单时间:  " + orderInfos.getCtime());
                    } else {
                        vh.tv_orderTime.setText("");
                    }
                    if (0 == orderInfos.getPoststatus()) {
                        vh.tv_prepare.setText("已备好");
                        vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.white));
                        vh.tv_prepare.setBackgroundResource(R.drawable.bg_orange_soild);
                        vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认执行此操作吗？")
                                        .setPositiveButton("确认执行", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Message message = new Message();
                                                message.what = 1;
                                                Bundle bundle = new Bundle();
                                                bundle.putString("orderid", orderInfos.getId());
                                                bundle.putString("shopid", orderInfos.getShopid());
                                                bundle.putInt("flag", 1);
                                                bundle.putInt("pos", position);
                                                message.setData(bundle);
                                                handler.sendMessage(message);
                                                dialog.dismiss();
                                            }
                                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();
                                            }
                                        }).show();
                                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                            }
                        });
                    } else if (1 == orderInfos.getPoststatus()) {
                        vh.tv_prepare.setText("去发货");
                        vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.white));
                        vh.tv_prepare.setBackgroundResource(R.drawable.bg_orange_soild);
                        vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Message message = new Message();
                                message.what = 2;
                                Bundle bundle = new Bundle();
                                bundle.putString("orderid", orderInfos.getId());
                                bundle.putInt("pos", position);
                                message.setData(bundle);
                                handler.sendMessage(message);
                            }
                        });
                    }
                    break;
                case 3:// 问题件   都是邮寄到
                    vh.rl_postInfo.setVisibility(View.VISIBLE);
                    vh.ll_complain.setVisibility(View.GONE);
                    vh.ll_total_postage.setVisibility(View.VISIBLE);
                    vh.rl_address_info.setVisibility(View.VISIBLE);
                    vh.tv_flag.setText("问题件");
                    vh.ll_base.setBackgroundResource(R.color.cart_background);
                    if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                        vh.tv_orderTime.setText("下单时间:  " + orderInfos.getCtime());
                    } else {
                        vh.tv_orderTime.setText("");
                    }
                    vh.tv_prepare.setText("去发货");
                    vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.white));
                    vh.tv_prepare.setBackgroundResource(R.drawable.bg_orange_soild);
                    vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Message message = new Message();
                            message.what = 2;
                            Bundle bundle = new Bundle();
                            bundle.putString("orderid", orderInfos.getId());
                            bundle.putInt("pos", position);
                            message.setData(bundle);
                            handler.sendMessage(message);
                        }
                    });
                    break;
                case 4:  // 被投诉  都是邮寄到
                    vh.rl_postInfo.setVisibility(View.VISIBLE);
                    vh.ll_complain.setVisibility(View.VISIBLE);
                    vh.ll_total_postage.setVisibility(View.VISIBLE);
                    vh.rl_address_info.setVisibility(View.VISIBLE);
                    if ("0".equals(orderInfos.getPoststatus())) {
                        vh.tv_flag.setText("待发货");
                    } else if ("1".equals(orderInfos.getPoststatus())) {
                        vh.tv_flag.setText("预约取件");
                    } else if ("2".equals(orderInfos.getPoststatus())) {
                        vh.tv_flag.setText(" 已发货");
                    } else if ("3".equals(orderInfos.getPoststatus())) {
                        vh.tv_flag.setText(" 已签收");
                    } else if ("5".equals(orderInfos.getPoststatus())) {
                        vh.tv_flag.setText(" 确认签收");
                    }
                    vh.ll_base.setBackgroundResource(R.color.cart_background);
                    if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                        vh.tv_orderTime.setText("下单时间:  " + orderInfos.getPaytime());
                    } else {
                        vh.tv_orderTime.setText("");
                    }
                    vh.tv_prepare.setText("已处理?");
                    vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.white));
                    vh.tv_prepare.setBackgroundResource(R.drawable.bg_orange_soild);
                    vh.tv_prepare.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认执行此操作吗？")
                                    .setPositiveButton("确认执行", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Message message = new Message();
                                            message.what = 3;
                                            Bundle bundle = new Bundle();
                                            bundle.putString("orderid", orderInfos.getId());
                                            bundle.putInt("pos", position);
                                            message.setData(bundle);
                                            handler.sendMessage(message);
                                            dialog.dismiss();
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).show();
                            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        }
                    });
                    break;
                case 5:
                    vh.ll_complain.setVisibility(View.GONE);
                    if (null != orderInfos.getPaytime() && !"".equals(orderInfos.getPaytime())) {
                        vh.tv_orderTime.setText("支付时间:  " + orderInfos.getPaytime());
                    } else {
                        vh.tv_orderTime.setText("");
                    }
                    vh.tv_prepare.setTextColor(context.getResources().getColor(R.color.postCartItem));
                    vh.tv_prepare.setText("已完结");
                    if (1 == orderInfos.getOrdertype()) {    //邮寄
                        vh.tv_prepare.setBackgroundResource(R.color.cart_background);
                        vh.rl_postInfo.setVisibility(View.VISIBLE);
                        vh.ll_total_postage.setVisibility(View.VISIBLE);
                        vh.rl_address_info.setVisibility(View.VISIBLE);
                        vh.ll_base.setBackgroundResource(R.color.cart_background);
                        if ("5".equals(orderInfos.getPoststatus())) {
                            vh.tv_flag.setText("已签收");
                        } else if ("6".equals(orderInfos.getPoststatus())) {
                            vh.tv_flag.setText("已评价");
                        }
                    } else {                  //自提
                        vh.rl_postInfo.setVisibility(View.GONE);
                        vh.ll_total_postage.setVisibility(View.GONE);
                        vh.rl_address_info.setVisibility(View.GONE);
                        vh.ll_base.setBackgroundResource(R.color.bg_time);
                        vh.tv_prepare.setBackgroundResource(R.color.bg_time);
                    }
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return view;
    }

    public void setOrderType(int ordertype) {
        this.ordertype = ordertype;
    }

    private class ViewHolder {
        private LinearLayout ll_base;
        private TextView tv_orderId;
        private TextView tv_orderNum;
        private TextView tv_orderTime;
        private TextView tv_prepare;

        private TextView tv_postId;
        private RelativeLayout rl_postInfo;
        private TextView tv_flag;
        private LinearLayout ll_complain;
        private TextView tv_complain_time;
        private TextView tv_complain;

        private NoScrollListView lvList;

        private TextView tv_md;
        private TextView tv_hd;
        private TextView tv_price;
        private TextView tv_total_postage;
        private LinearLayout ll_total_postage;
        private TextView tv_orderPhone;
        private TextView tv_call;

        private RelativeLayout rl_address_info;
        private TextView tvUserName;
        private TextView tvPhone;
        private TextView tvAddress;
    }
}
